//import CryptoJS from "crypto-js";
import Utf8 from "crypto-js/enc-utf8";
import Base64 from "crypto-js/enc-base64";
import Hex from "crypto-js/enc-hex";
import AES from "crypto-js/aes";
import ECB from "crypto-js/mode-ecb";
import Pkcs7 from "crypto-js/pad-pkcs7";

export function encode(words) {
    const key = Utf8.parse("1111111111111111");
    const srcs = Utf8.parse(words);
    var encrypted = AES.encrypt(srcs, key, {
        mode: ECB,
        padding: Pkcs7,
    });
    var hexStr = encrypted.ciphertext.toString();

    // console.log("hexStr : " + hexStr);
    //暂存hexStr，用以转base64
    var oldHexStr = Hex.parse(hexStr);
    // 将密文转为base64的字符串
    var base64Str = Base64.stringify(oldHexStr);
    //自行决定返回 base64Str 或 hexStr(解密时有所不同)

    return base64Str;
}

export function decode(word) {
    var key = Utf8.parse("1111111111111111");
    const deBase64Str = Base64.parse(word);
    // 转成 hex
    const encryptedHexStr = Hex.stringify(deBase64Str);

    let decryptedHexStr = Hex.parse(encryptedHexStr);
    let srcs = Base64.stringify(decryptedHexStr);
    let decrypt = AES.decrypt(srcs, key, {
        mode: ECB,
        padding: Pkcs7,
    });
    let decryptedStr = decrypt.toString(Utf8);
    var value = decryptedStr.toString();
    return value;
}
